package com.ruoyi.platform.system.user.service;

import com.ruoyi.base.constant.Constants;
import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;
import com.ruoyi.base.permission.TokenManager;
import com.ruoyi.base.service.BaseService;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.base.utils.md5.Md5Utils;
import com.ruoyi.platform.system.user.dao.UserDAO;
import com.ruoyi.platform.system.user.manager.UserManager;
import com.ruoyi.platform.system.user.pojo.UserDO;
import com.ruoyi.platform.system.verify.manager.VerifyCodeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 密码管理
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-13 05:40:22
 */
@Slf4j
@Service
public class PasswordService extends BaseService {
    @Autowired
    private UserDAO sysUserDao;
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private UserManager userManager;
    @Autowired
    private VerifyCodeManager verifyCodeManager;

    /**
     * 重置密码
     */
    public void resetPwd(long userId) {
        userManager.checkUserId(userId);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(userId, Constants.DEFAULT_PASSWORD));
        //刷新登陆缓存
        tokenManager.refreshCache(userId);
    }

    /**
     * 根据账号密码修改密码
     */
    public void resetPwd(String account, String old, String pwd) {
        UserDO exist = sysUserDao.getByAccount(account);
        if (exist == null) {
            throw exception(ExceptionEnum.ACCOUNT_OR_PWD_WRONG);
        }
        //检查旧密码
        boolean isRight = Md5Utils.matchesPassword(old, exist.getPassword());
        if (!isRight) {
            throw exception(ExceptionEnum.ACCOUNT_OR_PWD_WRONG);
        }

        if (Objects.equals(old, pwd)) {
            throw new SysException(ExceptionEnum.NEW_PWD_THE_SAME_AS_OLD);
        }
        //检查密码合法性
        InputCheckUtils.checkPwd(pwd);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(exist.getUserId(), pwd));
    }

    /**
     * 根据短信验证码修改密码
     */
    public void resetPwdByCode(String phone, String pwd, String code) {
        //检查验证码
        verifyCodeManager.checkPhoneVerifyCode(phone, code);

        UserDO exist = sysUserDao.getByPhone(phone);
        if (exist == null) {
            throw exception(ExceptionEnum.PHONE_NOT_EXISTS);
        }
        //检查密码合法性
        InputCheckUtils.checkPwd(pwd);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(exist.getUserId(), pwd));
    }

    /**
     * 修改我的密码
     */
    public void resetPwd(String pwd) {
        UserDO exist = userManager.checkUserId(tokenManager.getLoginUserId());
        if (Objects.equals(exist.getPassword(), Md5Utils.encryptPassword(pwd))) {
            throw new SysException(ExceptionEnum.NEW_PWD_THE_SAME_AS_OLD);
        }
        //检查密码合法性
        InputCheckUtils.checkPwd(pwd);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(exist.getUserId(), pwd));
    }

    /**
     * 修改用户密码
     */
    public void editUserPwd(long userId, String pwd) {
        userManager.checkUserId(userId);
        //检查密码合法性
        InputCheckUtils.checkPwd(pwd);

        sysUserDao.updateUserByPrimaryKeySelective(new UserDO(userId, pwd));
    }
}